package eu.boss.activities;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.TextView;
import eu.boss.R;
import eu.boss.controller.ShadowStatsController;
import eu.boss.model.Player;

/**
 * Activity displaying stats about teams and best player of the group
 * @author Arnaud
 *
 */
public class TeamStatsActivity extends Activity{

	private ShadowStatsController mController;
	private TextView bNS; //Best Name Shadow
	private TextView bNH; //Best Name Hunter
	private TextView bNN; //Best Name Neutral
	private TextView bRS; //Best Ratio Shadow
	private TextView bRH; //Best Ratio Hunter
	private TextView bRN; //Best Ratio Neutral
	private TextView SW; //Shadow Wins
	private TextView SL; //Shadow Losts
	private TextView HW; 
	private TextView HL;
	private TextView NW;
	private TextView NL;
	
	private ServiceConnection mControllerConnection = new ServiceConnection() {
		// Called when the connection with the service is established
		public void onServiceConnected(ComponentName className, IBinder service) {
			mController = ((ShadowStatsController.LocalBinder) service)
					.getService();
			displayResults();
		}

		// Called when the connection with the service disconnects unexpectedly
		public void onServiceDisconnected(ComponentName className) {
			mController = null;
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.teamstats);
		
		//initialize components
		bNS=(TextView) findViewById(R.id.best_name_s);
		bNH=(TextView) findViewById(R.id.best_name_h);
		bNN=(TextView) findViewById(R.id.best_name_n);
		bRS=(TextView) findViewById(R.id.best_ratio_s);
		bRH=(TextView) findViewById(R.id.best_ratio_h);
		bRN=(TextView) findViewById(R.id.best_ratio_n);
		
		SW=(TextView) findViewById(R.id.tvSWs);
		SL=(TextView) findViewById(R.id.tvSLs);
		HW=(TextView) findViewById(R.id.tvHWs);
		HL=(TextView) findViewById(R.id.tvHLs);
		NW=(TextView) findViewById(R.id.tvNWs);
		NL=(TextView) findViewById(R.id.tvNLs);
		
	}
	
	
	@Override
	protected void onStart() {
		super.onStart();
		// new intent to bind service
		Intent intent = new Intent(this, ShadowStatsController.class);
		getApplicationContext().bindService(intent, mControllerConnection, Context.BIND_AUTO_CREATE);
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		if (mController!=null)
			displayResults();
	}
	
	/**
	 * Displays results in dedicated fields
	 */
	public void displayResults()
	{
		String bestS="";
		String bestH="";
		String bestN="";
		float bestRatioS=(float) 0.00;
		float bestRatioH=(float) 0.00;
		float bestRatioN=(float) 0.00;
		Player p=null;
		
		for (int i=0; i<mController.getPlayersList().getPlayers().size(); i++)
		{
			p=mController.getPlayersList().getPlayers().get(i);
			if (Float.valueOf(p.getRatioH())>bestRatioH)
			{
				bestRatioH=Float.valueOf(p.getRatioH());
				bestH=p.getName();
			}
			if (Float.valueOf(p.getRatioN())>bestRatioN)
			{
				bestRatioN=Float.valueOf(p.getRatioN());
				bestN=p.getName();
			}
			if (Float.valueOf(p.getRatioS())>bestRatioS)
			{
				bestRatioS=Float.valueOf(p.getRatioS());
				bestS=p.getName();
			}
		}
		
		bNS.setText(bestS);
		bNH.setText(bestH);
		bNN.setText(bestN);
		bRS.setText(""+bestRatioS);
		bRH.setText(""+bestRatioH);
		bRN.setText(""+bestRatioN);
		
	}
}
